package app.displayTree.special.sub
{
    import app.debug.APPLog;
    import app.displayTree.analyze.DisplayTreeAnalyzeSupport;
    import app.utils.MetaInfoUtils;
    import app.utils.SWFInfoTreeImageUtils;

    import copyengine.infoTree.meta.CES3ScaleImageMeta;
    import copyengine.infoTree.meta.CESDisplayObjectMeta;
    import copyengine.utils.CEStringUtils;

    import flash.display.Bitmap;
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;
    import flash.geom.Rectangle;

    /**
     * 当一个元件导出类以S3_或者s3_开头时候则该元件当做S3元件处理
     * 比如一个Mc导出类叫做S3_GreenButtonBg 则该元件会被当做S3类型元件处理 且
     * 后续碰到Library中以GreenButtonBg_开头的元件 都会被该Handle类处理
     * 比如GreenButtonBg_A1 GreenButtonBg_A2 等等
     *
     * 每一个S3元件内部
     * 1` 应该包含且仅包含两个元件 一个元件名字为mc作为需要S3的元件
     *    另外一个元件名字h表示为水平缩放 或者名字v表示垂直缩放
     *
     * 2` 元件均以(0,0)对齐 因为计算mFirstRegionSize和mSecondRegionSize都是以00点计算的
     *
     */
    public class S3ScaleSpecialSymbolHandle extends SpecialSymbolHandleBasic
    {

        //当前S3元件的前缀名
        private var mS3SymbolNamePR:String;

        private var mS3SymbolBitmap:Bitmap;
        private var mS3SymbolDirection:int;
        private var mFirstRegionSize:Number;
        private var mSecondRegionSize:Number;

        public function S3ScaleSpecialSymbolHandle() {
        }


        override protected function initialize(_symbolKey:String, _symbol:DisplayObject):void {
            mS3SymbolNamePR = CEStringUtils.afterFirst(_symbolKey, "_");

            var s3Symbol:DisplayObject = (_symbol as DisplayObjectContainer).getChildByName("mc");
            var hMc:DisplayObject = (_symbol as DisplayObjectContainer).getChildByName("h");
            var vMc:DisplayObject = (_symbol as DisplayObjectContainer).getChildByName("v");
            if (s3Symbol == null || (hMc == null && vMc == null)) {
                APPLog.err("S3 symbol : " + _symbolKey + " not contain child named with `mc` as target or not contain `h`or`v` as reference object ");
                return;
            }

            if (hMc != null) {
                mFirstRegionSize = hMc.x;
                mSecondRegionSize = hMc.width;
                mS3SymbolDirection = CES3ScaleImageMeta.DIRECTION_HORIZONTAL;
            } else {
                mFirstRegionSize = vMc.y;
                mSecondRegionSize = vMc.height;
                mS3SymbolDirection = CES3ScaleImageMeta.DIRECTION_VERTICAL;
            }
            mS3SymbolBitmap = new Bitmap(SWFInfoTreeImageUtils.cacheDisplayObjectToBitmapData(s3Symbol));
        }

        override protected function isCanHandleThisSpecialSymbolKey(_symbolKey:String):Boolean {
            return CEStringUtils.beginsWith(_symbolKey, "S3_") || CEStringUtils.beginsWith(_symbolKey, "s3_");
        }

        override protected function isSpecialSymbol(_symbol:Sprite):Boolean {
            //按注释中的例子来说的话此时mS3SymbolNamePR = GreenButtonBg,而需要beginsWith的Key
            //应该为GreenButtonBg_ 这样防止GreenButtonBgXXX这样的名字被误操作
            return CEStringUtils.beginsWith(getMcAs3ExportName(_symbol), mS3SymbolNamePR + "_");
        }

        override protected function doGenerateSpecialSymbolMeta(_symbol:Sprite, _support:DisplayTreeAnalyzeSupport):CESDisplayObjectMeta {
            var scale3Meta:CES3ScaleImageMeta = new CES3ScaleImageMeta();
            MetaInfoUtils.fillDisplayObjectMetaBasicInfo(_symbol, scale3Meta);

            /**
             * S3 S9的Meta需要重新修正宽高
             *
             * 因为S3和S9不像普通的Mc那样 当宽高变换时候为Scale调整, (S3,S9的Scale恒定为1)
             * 所以当在FLA中对S3.S9元件进行旋转操作时候,如果直接通过fillDisplayObjectMetaBasicInfo函数读取的宽高(其实读取的是矩阵) 得到的宽高
             * 是 _symbol.getBounds(_symbol.parent)的结果,  比如mc的高度是10,当期Rotation不为0时候,读到的高度不为10(可以查看Flash的属性面板 里面的高度会变)
             */
            var bounds:Rectangle = _symbol.getBounds(_symbol);
            scale3Meta.width = bounds.width;
            scale3Meta.height = bounds.height;

            scale3Meta.textureMeta = _support.pushMcToExportAndReturnMcTextureInfo(mS3SymbolBitmap);
            scale3Meta.direction = mS3SymbolDirection;
            scale3Meta.firstRegionSize = mFirstRegionSize;
            scale3Meta.secondRegionSize = mSecondRegionSize;

            return scale3Meta;
        }
    }

}
